package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysBuildingMapper;
import com.ruoyi.system.mapper.SysHouseholdMapper;
import com.ruoyi.system.service.ISysBuildingService;
import com.ruoyi.system.service.ISysCommunityService;
import com.ruoyi.system.service.ISysHouseholdTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysNucleicAcidTaskMapper;
import com.ruoyi.system.service.ISysNucleicAcidTaskService;

/**
 * 楼栋核酸任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-16
 */
@Service
public class SysNucleicAcidTaskServiceImpl implements ISysNucleicAcidTaskService 
{
    @Autowired
    private SysNucleicAcidTaskMapper sysNucleicAcidTaskMapper;

    @Autowired
    private ISysCommunityService sysCommunityService;

    @Autowired
    private ISysBuildingService sysBuildingService;

    @Autowired
    private SysBuildingMapper sysBuildingMapper;

    @Autowired
    private SysHouseholdMapper householdMapper;

    @Autowired
    private ISysHouseholdTaskService sysHouseholdTaskService;

    /**
     * 查询楼栋核酸任务
     * 
     * @param id 楼栋核酸任务主键
     * @return 楼栋核酸任务
     */
    @Override
    public SysNucleicAcidTask selectSysNucleicAcidTaskById(Long id)
    {
        return sysNucleicAcidTaskMapper.selectSysNucleicAcidTaskById(id);
    }

    /**
     * 查询楼栋核酸任务列表
     * 
     * @param sysNucleicAcidTask 楼栋核酸任务
     * @return 楼栋核酸任务
     */
    @Override
    public List<SysNucleicAcidTask> selectSysNucleicAcidTaskList(SysNucleicAcidTask sysNucleicAcidTask)
    {
        return sysNucleicAcidTaskMapper.selectSysNucleicAcidTaskList(sysNucleicAcidTask);
    }

    /**
     * 新增楼栋核酸任务
     * 
     * @param sysNucleicAcidTask 楼栋核酸任务
     * @return 结果
     */
    @Override
    public int insertSysNucleicAcidTask(SysNucleicAcidTask sysNucleicAcidTask)
    {
        // 获取前端传递的参数
        Long communityId = sysNucleicAcidTask.getCommunity();
        // 查询小区名
        SysCommunity sysCommunity = sysCommunityService.selectSysCommunityById(communityId);
        String communityName = sysCommunity.getName();
        // 修改小区状态
        sysCommunity.setState(1);
        sysCommunityService.updateSysCommunity(sysCommunity);

        // 查询楼栋名
        Long buildingId = sysNucleicAcidTask.getBuilding();
        SysBuilding sysBuilding = sysBuildingService.selectSysBuildingById(buildingId);
        String building = sysBuilding.getBuilding();

        // 修改楼栋状态
        sysBuilding.setState(1);
        sysBuildingService.updateSysBuilding(sysBuilding);

        // 转换时间格式
        Date starttime = sysNucleicAcidTask.getStarttime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(starttime);
        // 任务名
        sysNucleicAcidTask.setTask(communityName + building + format);
        // 状态
        sysNucleicAcidTask.setState(0);

        // 人数
        Integer total = householdMapper.getTotal(buildingId);
        sysNucleicAcidTask.setTotal(total);
        // 完成人数
        sysNucleicAcidTask.setComplete(0);

        // 楼栋所有人的状态设置为未检测   发送短信
        List<SysHousehold> householdList = householdMapper.selectHouseholdList(buildingId);
        for (SysHousehold household:householdList) {
            household.setState(0);
            String phone = household.getPhone();
            System.out.println(phone); // 模拟发送短信
            householdMapper.updateSysHousehold(household);

            // 添加到住户核酸任务
            SysHouseholdTask sysHouseholdTask = new SysHouseholdTask();
            sysHouseholdTask.setTask(communityName + building + format);
            sysHouseholdTask.setState(0);
            sysHouseholdTask.setName(household.getName());
            sysHouseholdTask.setStartTime(starttime);
            sysHouseholdTask.setOutTime(sysNucleicAcidTask.getOuttime());
            sysHouseholdTask.setPosition(sysNucleicAcidTask.getPlaces());
            sysHouseholdTask.setCommunity(communityId);
            sysHouseholdTask.setBuilding(buildingId);

            sysHouseholdTaskService.insertSysHouseholdTask(sysHouseholdTask);

        }




        return sysNucleicAcidTaskMapper.insertSysNucleicAcidTask(sysNucleicAcidTask);
    }

    /**
     * 修改楼栋核酸任务
     * 
     * @param sysNucleicAcidTask 楼栋核酸任务
     * @return 结果
     */
    @Override
    public int updateSysNucleicAcidTask(SysNucleicAcidTask sysNucleicAcidTask)
    {
        return sysNucleicAcidTaskMapper.updateSysNucleicAcidTask(sysNucleicAcidTask);
    }

    /**
     * 批量删除楼栋核酸任务
     * 
     * @param ids 需要删除的楼栋核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteSysNucleicAcidTaskByIds(Long[] ids)
    {
        return sysNucleicAcidTaskMapper.deleteSysNucleicAcidTaskByIds(ids);
    }

    /**
     * 删除楼栋核酸任务信息
     * 
     * @param id 楼栋核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteSysNucleicAcidTaskById(Long id)
    {
        return sysNucleicAcidTaskMapper.deleteSysNucleicAcidTaskById(id);
    }
}
